import {Component} from 'angular2/core';
import {UserService} from '../services/user.service';
import {User} from '../models/user';
import {TaskInfo} from '../models/taskInfo';
import {AddTaskInfoService} from '../services/add-task.service';

@Component({
  selector: 'add-task',
  providers: [AddTaskInfoService],
  templateUrl: 'app/components/add-task.component.html'
})
export class AddTaskInfo {
  newTaskInfo: TaskInfo = new TaskInfo();
  taskInfoAdded: boolean = false;
  user: User;

  constructor (
    private addTaskInfoService: AddTaskInfoService,
    private userService: UserService
  ) {}

  onSubmit() {
    this.userService.getUserByName(localStorage.getItem("currentUserName")).subscribe(
      user => {
        this.user = JSON.parse(JSON.parse(JSON.stringify(user))._body);
        console.log(this.user);
        this.newTaskInfo.userId = this.user.userId;
        this.addTaskInfoService.sendTask(this.newTaskInfo)
        .subscribe(
          data => {
            this.taskInfoAdded = true;
            this.newTaskInfo = new TaskInfo();
          },
          error => console.log(error)
        );
      },
      error => console.log(error)
    )
  }
}
